<template>
  <div class="base-visitor-apply">
    <van-empty v-if="isBuilding !== 0" description="当前无住房" />
    <div
      v-else
      style="height: 100%;"
    >
      <van-field
        readonly
        clickable
        :value="startTime"
        label="开始时间"
        placeholder="选择日期"
        @click="showCalendar = true"
      />
      <van-calendar v-model="showCalendar" @confirm="onConfirm" get-container="#app" />
      <van-field
        readonly
        clickable
        :value="endTime"
        label="结束时间"
        placeholder="选择日期"
        @click="showCalendar1 = true"
      />
      <van-calendar v-model="showCalendar1" @confirm="onConfirm1" get-container="#app" />

      <van-field
        v-model="vname"
        required
        label="访客姓名"
        placeholder="请输入访客姓名"
      />
      <van-field
        v-model="vtel"
        required
        label="手机号"
        placeholder="请输入手机号"
      />
      <van-field
        v-model="vidcard"
        required
        label="身份证号"
        placeholder="请输入身份证号"
      />

    <van-field
      type="textarea"
      v-model="reason"
      placeholder="申请理由"
      style="margin-top: 20px;"
    />
    <van-button
      round block type="info"
      style="margin-top: 20px;"
      @click="onSubmit"
    >{{ buttonMsg }}</van-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'VisitorApply',
  data() {
    return {
      isBuilding: 0,
      startTime: '',
      startGetTime:'',
      endTime:'',
      endGetTime:'',
      showCalendar: false,
      showCalendar1: false,
      vname:'',
      vtel:'',
      vidcard:'',
      reason:'',
      buttonMsg:"提交"
    };
  },
  mounted() {
    this.HttpManager.getBuilddingByUid(sessionStorage.getItem("userid")).then(res => {
      this.$toast.clear(true);
      if (res.code === 12006) {
        this.isBuilding = 1;
      } else if (res.code === 21009) {
        this.$notify({type: "danger", message: res.msg});
        this.isBuilding = 1;
      } else if (res.code === 21010) {
        this.$notify({type: "warning", message: res.msg});
        this.isBuilding = 1;
      }
    });
    this.HttpManager.getVisitorByUid(sessionStorage.getItem("userid")).then(res => {
      console.log(res);
      this.$toast.clear(true);
      if(res.code === 0){
        this.$notify({ type: "success", message: "上个访客申请已通过!"});
      }
      else if(res.code === 200009){
        this.$notify({ type: "warning", message: res.msg});
        this.buttonMsg = "修改";
        this.startTime = res.data.starttime;
        this.startGetTime = res.data.temp1;
        this.endGetTime = res.data.temp2;
        this.endTime = res.data.endtime;
        this.vname = res.data.vname;
        this.vtel = res.data.vtel;
        this.vidcard = res.data.vidcard;
        this.reason = res.data.reason;
      }
      else if(res.code === 200010){
        this.$notify({ type: "danger", message: res.msg});
        this.buttonMsg = "修改";
        this.startTime = res.data.starttime;
        this.startGetTime = res.data.temp1;
        this.endGetTime = res.data.temp2;
        this.endTime = res.data.endtime;
        this.vname = res.data.vname;
        this.vtel = res.data.vtel;
        this.vidcard = res.data.vidcard;
        this.reason = res.data.reason;
      }
    });
  },
  methods: {
    onSubmit () {
      if(this.buttonMsg === "提交"){
        this.HttpManager.visitor_insert({
          "uid":sessionStorage.getItem("userid"),
          "starttime":this.startGetTime,
          "endtime":this.endGetTime,
          "vname":this.vname,
          "vtel":this.vtel,
          "vidcard":this.vidcard,
          "reason":this.reason,
        }).then(res =>{
          if(res.code === 0)
            this.$notify({ type: "success", message: "申请成功"});
          else
            this.$notify({ type: "danger", message: "申请失败"});
        })
      }
      else if(this.buttonMsg === "修改"){
        this.HttpManager.visitor_update({
          "uid":sessionStorage.getItem("userid"),
          "starttime":this.startGetTime,
          "endtime":this.endGetTime,
          "vname":this.vname,
          "vtel":this.vtel,
          "vidcard":this.vidcard,
          "reason":this.reason,
        }).then(res =>{
          if(res.code === 0)
            this.$notify({ type: "success", message: res.msg});
          else
            this.$notify({ type: "danger", message: "修改失败"});
        })
      }
    },
    onConfirm (date) {
      this.startTime = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
      this.startGetTime = date.getTime();
      this.showCalendar = false;
    },
    onConfirm1 (date) {
      this.endTime = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
      this.endGetTime = date.getTime();
      this.showCalendar1 = false;
    },
  },
}
</script>

<style lang="less" scoped>

</style>
